#include<stdio.h>
#include<string.h>
#include "create_xml.h"

int num = 0;
int flag = 1;

void init()
{
	printf("---Student management System---\n");
	printf("==========1.新增学生==========\n");
	printf("==========2.删除学生==========\n");
	printf("==========3.打印学生==========\n");
	printf("==========4.填写成绩==========\n");
	printf("==========5.打印排名==========\n");
	//printf("==========6.查找电话==========\n");
	printf("==========7.退    出==========\n");
}

void New()
{
	if (num == 30) 
	{
		printf("This class is full!\n");
		return;
	}
	printf("请输入学生信息(ID 姓名 性别 语文 数学 英语):");
	scanf("%d%s%s%d%d%d",&student[num].ID,student[num].name,student[num].sex,&student[num].chinese,&student[num].math,&student[num].english);
	student[num].sum = student[num].chinese + student[num].math + student[num].english;
	num++;
}
void swap(int n)
{
	int i;
	for (i = n; i < num - 1; i++)
	{
		student[i].ID = student[i+1].ID;
		strcpy(student[i].name,student[i+1].name);
		strcpy(student[i].sex,student[i+1].sex);
		student[i].chinese = student[i+1].chinese;
		student[i].math = student[i+1].math;
		student[i].english = student[i+1].english;
	}
	num--;
}
void del()
{
	int i;
	int id;
	printf("请输入想要删除的学生的ID:");
	scanf("%d",&id);
	for (i = 0; i < num; i++)
	{
		if(id == student[i].ID) 
		{
			swap(i);
			break;
		}
	}
}

void print()
{
	int i;
	if (num == 0) 
	{
		printf("There are no students in the class.\n");
		return;
	}
	printf("学生信息:\n");
	printf("ID	名字    性别       语文 数学 英语 总分\n");
	for (i = 0; i < num; i++)
	{
		printf("%d	%s	%s	   %d    %d    %d	  %d\n",student[i].ID,student[i].name,student[i].sex,student[i].chinese,student[i].math,student[i].english,student[i].sum);
	}
}

void fill()
{
	int i;
	for (i = 0; i < num; i++)
	{
		printf("%s :",student[i].name);
		scanf("%d %d %d",&student[i].chinese,&student[i].math,&student[i].english);
		student[i].sum = student[i].chinese + student[i].math + student[i].english;
	}
}

void Printrank()
{
	int i,j;
	int a[num];
	int temp;
	for (i = 0; i < num; i++)
	{
		a[i] = i;
	}
	for (i = 0; i < num-1; i++)
	{
		for (j = 0; j < num - i - 1; j++)
		{
			if(student[j].sum < student[j+1].sum)
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}
	printf("排名  ID   名字  语文 数学 英语 总分\n");
	for (i = 0; i < num; i++)
	{
		printf("%d    %d    %s    %d    %d  %d  %d\n",i+1,student[a[i]].ID,student[a[i]].name,student[a[i]].chinese,student[a[i]].math,student[a[i]].english,student[a[i]].sum);
	}
}

/*void searchphone()
{
	int i;
	char ch[10];
	printf("请输入想要查找的学生名字:");
	scanf("%s",ch);
	for (i = 0; i < num; i++)
	{
		if (strcmp(ch,student[i].name) == 0)
		{
			printf("%s 电话:%s\n",student[i].name,student[i].tel);
			break;
		}
	}
}*/
int main()
{
	int n;
	//loding();
	while(flag)
	{
		init();
		scanf("%d",&n);
		switch (n)
		{
			case 1: New();			break;  //新增学生
			case 2: del();			break;  //删除学生
			case 3: print();		break;  //打印学生
			case 4: fill();			break;  //填写成绩
			case 5: Printrank();	break;  //打印排名
			//case 6: searchphone();	break;	//查找电话
			case 7: create_xml(num);
					flag = 0;		break;  //退出
		}
	}
}
